package com.dragon.modules.yuegou.client.wechat_mini.client.impl;

import com.dragon.modules.yuegou.client.wechat_mini.client.SubscribeMessageClient;
import com.dragon.modules.yuegou.client.wechat_mini.common.BaseResponse;
import com.dragon.modules.yuegou.client.wechat_mini.conn.IMiniClient;
import com.dragon.modules.yuegou.client.wechat_mini.request.SubscribeMessageRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.TemplateAddRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.TemplateDeleteRequest;
import com.dragon.modules.yuegou.client.wechat_mini.request.TemplateTitleListRequest;
import com.dragon.modules.yuegou.client.wechat_mini.response.CategoryResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.KeywordsResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.TemplateIdResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.TemplateResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.TemplateTitleResponse;

public class SubscribeMessageClientImpl implements SubscribeMessageClient {

	private final IMiniClient client;

	public SubscribeMessageClientImpl(IMiniClient client) {
		this.client = client;
	}
	
	@Override
	public BaseResponse send(String accessToken, SubscribeMessageRequest message) {
		return client.post("/cgi-bin/message/subscribe/send")
				.accessToken(accessToken)
				.json(message)
				.execute(BaseResponse.class);
	}

	@Override
	public TemplateIdResponse addTemplate(String accessToken, TemplateAddRequest request) {
		return client.post("/wxaapi/newtmpl/addtemplate")
				.accessToken(accessToken)
				.json(request)
				.execute(TemplateIdResponse.class);
	}

	@Override
	public BaseResponse deleteTemplate(String accessToken, TemplateDeleteRequest request) {
		return client.post("/wxaapi/newtmpl/deltemplate")
				.accessToken(accessToken)
				.json(request)
				.execute(BaseResponse.class);
	}

	@Override
	public CategoryResponse getCategory(String accessToken) {
		return client.get("/wxaapi/newtmpl/getcategory")
				.accessToken(accessToken)
				.execute(CategoryResponse.class);
	}

	@Override
	public KeywordsResponse getPubTemplateKeyWordsById(String accessToken, Integer tid) {
		// 此处接口文档与实际接口不一致
		return client.get("/wxaapi/newtmpl/getpubtemplatekeywords")
				.accessToken(accessToken)
				.parameter("tid", String.valueOf(tid))
				.execute(KeywordsResponse.class);
	}

	@Override
	public TemplateTitleResponse getPubTemplateTitleList(String accessToken, TemplateTitleListRequest request) {
		return client.get("/wxaapi/newtmpl/getpubtemplatetitles")
				.accessToken(accessToken)
				.parameter("ids", request.getIds())
				.parameter("start", request.getStart() != null ? request.getStart().toString() : "")
				.parameter("limit", request.getLimit() != null ? request.getLimit().toString() : "")
				.execute(TemplateTitleResponse.class);
	}

	@Override
	public TemplateResponse getTemplateList(String accessToken) {
		return client.get("/wxaapi/newtmpl/gettemplate")
				.accessToken(accessToken)
				.execute(TemplateResponse.class);
	}

}
